#include "widget.h"
#include "ui_widget.h"

#include <QDebug>
#include <QTimerEvent>
#include <QDateTime>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 开启定时器
    // this->startTimer(1000);

    connect(ui->startBtn, &QPushButton::clicked, [this](){
        // startTimer : 本质意义就是 每隔1s，执行一次 timerEvent 中的代码
        this->timer1 = this->startTimer(1000);
        this->timer2 = this->startTimer(2000);
    });

    // 停止定时器
    connect(ui->stopBtn, &QPushButton::clicked, [this](){
        this->killTimer(this->timer1);
        this->killTimer(this->timer2);
    });

    this->timer3 = this->startTimer(1000);
    ui->dateTimeEdit->setDateTime(QDateTime::currentDateTime());
}

void Widget::timerEvent(QTimerEvent *event)
{
    if(event->timerId() == this->timer1)
    {
        this->i++;
        ui->lcdNumber->display(this->i);
    }
    else if (event->timerId() == this->timer2)
    {
        this->n++;
        ui->lcdNumber_2->display(this->n);
    }
    else if (event->timerId() == this->timer3)
    {
        // 获取当前的时间
        ui->dateTimeEdit->setDateTime(QDateTime::currentDateTime());
    }
}

Widget::~Widget()
{
    delete ui;
}
